<?php


namespace app\test\controller;


use app\chat\LiveMessage;
use app\common\lib\apns\ApnsPush;
use app\common\model\UserPush;
use think\Controller;
use think\Env;
use \GatewayWorker\Lib\Gateway;
use app\common\lib\Util;
use think\Log;
use XznsTools\push\Jpush;

/**
 * TestWebsocket 测试
 * Class TestWebsocket
 * @package app\test\controller
 */
class TestWebsocket extends Controller
{


    /**
     * 发送助通短信测试
     * 请求地址：http://local-beta-wechat.goddessxzns.com/test/test_websocket/test_send_msg
     * @return \think\response\Json
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function test_send_msg(): \think\response\Json
    {
        if (Env::get('host.name') !== 'online')
        {
            try
            {
                $res = Util::sendHySms(['17333657763', '18511696867'], '', 62664, ['task_id' => '159649223376392']);
                return show(200, '发送短信结果', $res);
            }
            catch (\Exception $e)
            {
                return show(0, '发送短信失败', $e->getMessage());
            }
        } else
        {
            return show(0, '失败！');
        }
    }


    /**
     * 请求地址：
     * @return \think\response\Json
     */
    public function test()
    {
        if (Env::get('host.name') !== 'online')
        {
            $data = [];

            $registerAddress = config('websocket.register_ip') . ':' . config('websocket.register_port');
            try
            {
                $data['host_name'] = getHostName();
//            $data['host_ip'] = getHostByName(getHostName())
                //获取env
//            $data['envs'] = getenv();

                Gateway::$registerAddress = $registerAddress;
                //向所有人发送消息
                Gateway::sendToAll("test zhunle_api");
                Gateway::sendToAll("群发消息 zhunle_api");
                //向某个client_id发送数据
//            Gateway::sendToClient('7f00000108ff0000000a', '私聊数据');//注意，这个发送不成功会抛错
                $data['获取所有在线人数'] = Gateway::getAllClientIdCount();
                $data['getAllClientSessions'] = Gateway::getAllClientSessions();
                $data['getAllClientIdList'] = Gateway::getAllClientIdList();
                $data['获取全局所有在线group id列表'] = Gateway::getAllGroupIdList();
                $data['测试咨询师是否在线'] = LiveMessage::getUserIsOnline(2, 11);

                //测试-写日志
                Util::logWriteOfLiveChatMsg([
                    'live_id' => 'live_id',//直播场次的id（不能为空,string）
                    'user_id' => 'user_id zhunle_api',//用户的id（可以为空,string）
                    'msg_type' => 'msg_type',//发送的消息内容（不能为空，string,1：发送消息；2：发送礼物；...）
                    'msg_content' => 'msg_content',//发送的消息内容（不能为空，string）]);
                ]);

                return show(200, '成功 $registerAddress=' . $registerAddress, $data);
            }
            catch (\Exception $e)
            {
                return show(0, '失败 $registerAddress=' . $registerAddress, $e->getMessage());
            }
        } else
        {
            return show(0, '失败！');
        }
    }


    public function send()
    {
        if (Env::get('host.name') !== 'online')
        {
            $type = input('type');
            $live_id = input('live_id');
            $user_id = input('user_id');
            $client_id = input('client_id');
            $content = input('content');
            $data = json_decode($content, true);
            $data1 = json_decode($data, true);


            $who = LiveMessage::who(LiveMessage::WHO_TYPE_ALL, $live_id, $live_id);
            if ($type === LiveMessage::MSG_TYPE_ACTION_USER)
            {
                if ($user_id)
                {
                    $who = LiveMessage::who(LiveMessage::WHO_TYPE_UID, $live_id, $user_id);
                } else if ($client_id)
                {
                    $who = LiveMessage::who(LiveMessage::WHO_TYPE_CLIENT_ID, $live_id, $client_id);
                } else
                {
                    return show(0, '必须指定某个人 或 client_id！', $data1);
                }
            } else if ($type === LiveMessage::MSG_TYPE_POP_MSG_LIVER_ORDER)
            {
                $who = LiveMessage::who(LiveMessage::WHO_TYPE_LIVER, $live_id, $live_id);
            }

            //发送给所有人
            switch ($type)
            {
                case LiveMessage::MSG_TYPE_POP_MSG:
                {
                    $who['uid'] = 12345883;
                    $return = LiveMessage::sendMsg($who, $type, $data1['data'][$type]);
                    break;
                }
                case LiveMessage::MSG_TYPE_ONLINE_INFO:
                case LiveMessage::MSG_TYPE_POP_HEADER:
                case LiveMessage::MSG_TYPE_POP_GIFT:
                case LiveMessage::MSG_TYPE_POP_USER_COME:
                case LiveMessage::MSG_TYPE_POP_ZAN_SIGN:
                case LiveMessage::MSG_TYPE_POP_RECOMMEND:
                case LiveMessage::MSG_TYPE_POP_COUPON:
                case LiveMessage::MSG_TYPE_POP_AWARD_START:
                case LiveMessage::MSG_TYPE_POP_MSG_LIVER_ORDER:
                case LiveMessage::MSG_TYPE_CHANGE_CAMERA_RADIO:
                case LiveMessage::MSG_TYPE_ACTION_USER:
                case LiveMessage::MSG_TYPE_CLOSE_RECOMMEND_GOODS:
                case LiveMessage::MSG_TYPE_LIVE_STATUS:
                {
                    $return = LiveMessage::sendMsg($who, $type, $data1['data'][$type]);
                    break;
                }
                case 'send_zan_random':
                {//随机多次发送点赞数据

                    $send_zan_random_count = 0;
                    $send_zan_random_success = 0;
                    for ($i = 0; $i < $data1; $i++)
                    {
                        $return1 = LiveMessage::sendMsg($who, 'pop_zan_sign', $i % 12 + 1);
                        if ($return1['is_success'])
                        {
                            $send_zan_random_success++;
                        }
                        $send_zan_random_count++;
                    }

                    return show(200, '点赞了 ' . $send_zan_random_count . '个，成功了' . $send_zan_random_success . '个！', $data);
                }
                case 'send_user_com_random':
                {//随机多次发送“***来了”数据

                    $all_count = 0;
                    $success_count = 0;
                    for ($i = 0; $i < $data1; $i++)
                    {
                        $data = [
                            'user_info' => [
                                'uen' => '776d22bdafd9',
                                'name' => '用户名' . $i,
                                'avatar' => '/uploads/avatar/20200925/c6252be325da00742457beaf26c0ae42.png',
                                'vip_level' => $i % 8 + 1,
                                'rank_index' => $i % 10 + 1,
                            ],
                            'des' => '用户名' . $i . '进入直播间'
                        ];
                        $return1 = LiveMessage::sendMsg($who, 'pop_user_come', $data);
                        if ($return1['is_success'])
                        {
                            $success_count++;
                        }
                        $all_count++;
                    }

                    return show(200, '***来了，总共：' . $all_count . '个，成功了' . $success_count . '个！', $data);
                }
                case 'send_msg_random':
                {//随机多次发送“聊天室消息”数据
                    $all_count = 0;
                    $success_count = 0;
                    for ($i = 0; $i < $data1; $i++)
                    {
                        $data = [
                            'user_info' => [
                                'uen' => '776d22bdafd9',
                                'name' => '用户名' . $i,
                                'avatar' => '/uploads/avatar/20200925/c6252be325da00742457beaf26c0ae42.png',
                                'vip_level' => $i % 8 + 1,
                                'rank_index' => $i % 10 + 1,
                            ],
                            'msg_info' => [
                                'id' => $i,
                                'color' => '#ffffff',
                                'content' => '发送的聊天消息' . $i
                            ]

                        ];
                        $return1 = LiveMessage::sendMsg($who, 'pop_msg', $data);
                        if ($return1['is_success'])
                        {
                            $success_count++;
                        }
                        $all_count++;
                    }

                    return show(200, '发送的聊天消息，总共：' . $all_count . '个，成功了' . $success_count . '个！', $data);
                }
                case 'send_gift_random':
                {//随机多次发送“礼物”数据
                    $all_count = 0;
                    $success_count = 0;
                    for ($i = 0; $i < $data1; $i++)
                    {
                        $data = [
                            'user_info' => [
                                'uen' => '776d22bdafd9',
                                'name' => '用户名' . $i,
                                'avatar' => '/uploads/avatar/20200925/c6252be325da00742457beaf26c0ae42.png',
                                'vip_level' => $i % 8 + 1,
                                'rank_index' => $i % 10 + 1,
                            ],
                            'gift_info' => [
                                'id' => $i,
                                "name" => "送出礼物" . $i,
                                "count" => 10,
                                "img" => "/uploads/avatar/20200925/c6252be325da00742457beaf26c0ae42.png",
                                "coin" => 50 * ($i % 10),
                                "lottie" => [
                                    "url" => "/mini/divination_or_taro/taro_detail/lottie/data.json",
                                    "width_ratio_of_width" => 1,
                                    "height_ratio_of_width" => 1,
                                    "left_ratio_of_width" => 0,
                                    "bottom_ratio_of_width" => 0,
                                    "loop_num" => 2
                                ]
                            ]

                        ];
                        $return1 = LiveMessage::sendMsg($who, 'pop_gift', $data);
                        if ($return1['is_success'])
                        {
                            $success_count++;
                        } else
                        {
                            Log::write('send_gift_random错误消息：' . json_encode($return1), 'error');
                        }
                        $all_count++;
                    }

                    return show(200, '送出礼物消息，总共：' . $all_count . '个，成功了' . $success_count . '个！', $data);
                }
                case "test_close_group":
                {//模拟关闭聊天室，客户端重连ws测试（不需要登录，比如：并发量高了以后，弹出的服务器，自动释放的时候）
                    //设置注册地址
                    Gateway::$registerAddress = config('websocket.register_ip') . ':' . config('websocket.register_port');
//                    Gateway::ungroup($live_id);//这个只是解散分组，但是没有关闭客户端的ws连接
                    $clients = Gateway::getAllClientIdList();
                    foreach ($clients as $k => $v)
                    {
                        Gateway::closeClient($v);
                    }
                    return show(200, '所有ws连接都下线成功，总共' . count($clients) . '个连接！', $data1);
                    break;
                }
                case 'zyhs_send_user_com_random':
                {//准有好事-随机多次发送“***来了”数据

                    $all_count = 0;
                    $success_count = 0;
                    for ($i = 0; $i < $data1; $i++)
                    {
                        // 发送 进入房间消息
                        $return1 = LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_GOOD_RTC_LIVE_ROOM, 0, $live_id), LiveMessage::MSG_TYPE_POP_GOOD_RTC_LIVE_USER_JOIN, [
                            'user_info' => [
                                'name' => '用户名' . $i,
                            ],
                            'des' => '进入房间',
                        ]);

                        if ($return1['is_success'])
                        {
                            $success_count++;
                        }
                        $all_count++;
                    }

                    return show(200, '准有好事-随机多次发送“***来了”数据，总共：' . $all_count . '个，成功了' . $success_count . '个！', $data);
                }
                case 'zyhs_good_rtc_live_close':
                {
                    //准有好事-发送关播ws消息
                    $return1 = LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_GOOD_RTC_LIVE_ROOM, 0, $live_id), LiveMessage::MSG_TYPE_GOOD_RTC_LIVE_CLOSE, 1);
                    return show(200, '准有好事-发送关播ws消息：' . $return1, $data);
                }
                case 'zyhs_good_rtc_live_online_msg':
                {
                    //准有好事-发送更新在线人数ws消息
                    $return1 = LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_GOOD_RTC_LIVE_ROOM, 0, $live_id), LiveMessage::MSG_TYPE_GOOD_RTC_LIVE_ONLINE_MSG, '在线：' . $data1);
                    return show(200, '准有好事-发送更新在线人数ws消息：' . $return1, $data);
                }


                default:
                    return show(0, '类型错误！', $data1);
            }

            if ($return['is_success'])
            {
                return show(200, '发送成功！', $data1);
            }

            return show(0, $return['msg'], $data1);

        }

        return show(0, '发送失败！');
    }


    /**
     * 查询regis db性能
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function db()
    {
        if (Env::get('host.name') !== 'online')
        {
            $regids_many = [
                "13165ffa4ea396d5c74", "161a3797c8aca2c79c1", "1517bfd3f7141c475df", "1517bfd3f7138d10ee1", "121c83f760f0cfe7d70", "1517bfd3f7134f802b0", "141fe1da9e7cb986f60", "13165ffa4edcf9b1d96", "18171adc038116a9a2e", "141fe1da9e7ff779b7c", "1114a897927d4c589a4", "101d855909935b67b90", "1517bfd3f713e6561ee", "141fe1da9e7ff3a47e5", "1a1018970a7206dab4b", "13165ffa4ea25bfb900", "141fe1da9e7cb000ec6", "1517bfd3f7109e90711", "18171adc03d8faa321e", "101d8559099024ba86f", "121c83f760f3d957fa3", "1517bfd3f713d64089d", "161a3797c8d1a1c4725", "1114a897927b988a056", "1517bfd3f71085b3ee9", "1a1018970a716010d06", "1517bfd3f7131f0b6ee", "101d85590992e76df1c", "1a1018970a7250a08b4", "161a3797c8ac7ea0143", "121c83f7608e0c6a759", "1114a897927c81ccf2c", "171976fa8a54fb684fc", "161a3797c8d05b70ebb", "18171adc03d972e4018", "13165ffa4edce463acd", "121c83f760f0ba7db3e", "18171adc03d918423ad", "171976fa8a546a7d786", "13165ffa4edd5042955", "13165ffa4ea65bc8e53", "171976fa8a2493ef538", "1a1018970a03ef5409d", "121c83f7608aa8c4166", "13165ffa4ea1157fd7f", "1a1018970a0ec62edd5", "101d855909930bbfb00", "191e35f7e0ed2ea7c4a", "141fe1da9e0e1871445", "1114a897920326a3820", "1517bfd3f76205a1e41", "1517bfd3f76e6306414", "171976fa8a2908c8c95", "101d855909e95c015e3", "161a3797c8aa8e8413f", "1114a8979207f4542b9", "141fe1da9e755a11c8e", "191e35f7e0ec77ea97e", "101d855909ed29d9cd4", "141fe1da9e07714fb40", "18171adc03a8787c7e7", "1114a897920032ba3e6", "18171adc03df4af0eb1", "121c83f7608ed452cb0", "18171adc03a41d08b22", "141fe1da9e00c59208b", "161a3797c8af1a41b15", "1a1018970a760aff69c", "191e35f7e0e071c3fae", "1114a89792035af6b9f", "141fe1da9e074a32ba4", "161a3797c8a8947b97f", "121c83f7608a767afa6", "171976fa8a2f7f82540", "191e35f7e0e01c66f44", "1a1018970a0abd117ef", "18171adc03a47492b01", "13165ffa4eb5cc36c4d", "141fe1da9e03014a560", "13165ffa4ea444f1be0", "18171adc03a2f26bfe4", "13165ffa4eade6571c4", "1517bfd3f768b70c078", "13165ffa4ea40157919", "171976fa8a2b249e887", "191e35f7e0ea9857709", "121c83f760f1c507f03", "121c83f7608bf12c010", "18171adc03a64d1b079", "1517bfd3f76dd72815f", "191e35f7e0ee233ab11", "121c83f7608ec8eee00", "191e35f7e0e9e766cd1", "121c83f7608bef0f933", "171976fa8a2adc31b7e", "101d855909e810cb026", "191e35f7e0ede2db324", "13165ffa4ea7be4eeaa", "13165ffa4ea7788a3e1", "1a1018970a0949ef916", "121c83f7608e91b7c3a", "101d85590996f8ac0de", "18171adc03a4c0815a1", "13165ffa4eaa980aa25", "18171adc03a4c029fd1", "1114a89792020f44d3a", "171976fa8a2f61fa289", "13165ffa4ea7b8479a1", "101d855909ec7ce66fe", "13165ffa4ea4d345718", "18171adc03a6a57a3f4", "13165ffa4ea6d4eb72c", "1114a8979207eee15d6", "1517bfd3f76dcfdece0", "161a3797c8a12941691", "18171adc03a7aeafeb8", "18171adc03a7aea912e", "1114a897920575ab19a", "141fe1da9e0139e3cff", "1517bfd3f769c475a36", "1517bfd3f76b0714dd5", "13165ffa4ea0b06d81a", "1114a897920663564e4", "171976fa8a2580dfe21", "171976fa8a25ef737c6", "191e35f7e0ee2a78cc5", "141fe1da9e0253f0aa3", "18171adc03a403f8edc", "1a1018970a0d9aea961", "161a3797c8acccc07bd", "1114a89792004ff0bfe", "18171adc03a1cfebbf8", "121c83f7608f60ea63d", "13165ffa4ea3d382e95", "101d855909ebbb93049", "13165ffa4ea7759fe2d", "161a3797c8acccf476e", "1517bfd3f76b0755402", "121c83f76088fece1bc", "101d855909ebbbb3d1b", "171976fa8a2c40c5cde", "1a1018970a7d37e2543", "1a1018970a0d9a08790", "1517bfd3f7686447a59", "1a1018970a09e20e480", "1114a8979201cacaa6a", "121c83f7608f802eccc", "1114a897920583838e6", "121c83f7608a6580ffe", "191e35f7e0ebcfa504c", "1517bfd3f76e8669790", "141fe1da9e04b7913a8", "141fe1da9e0ef6b3ac9", "141fe1da9e0417752d6", "121c83f76081a644330", "121c83f76088febad6e", "1114a89792066cccaca", "161a3797c8aaebfecbc", "18171adc03a464d3cfa", "121c83f7608a65b8a4f", "1114a89792004f6ef08", "1517bfd3f76f03481f5", "1a1018970a0d9a5d350", "1114a897920028bce75", "121c83f7608f3fa6f36", "1517bfd3f76d2a35835", "1114a897920028bd8ee", "161a3797c8ab695a25b", "171976fa8a2b17ae6a6", "18171adc03a7afced7b", "1a1018970a0e4719117", "13165ffa4ea714625c3", "141fe1da9e0ef683221", "1a1018970a036d61964", "171976fa8a29eba32ee", "191e35f7e0eaec72899", "121c83f7608b4653600", "191e35f7e0eaec567a6", "171976fa8a28aa2248b", "18171adc03a1cf84737", "141fe1da9e07add6a52", "141fe1da9e07adc42eb", "121c83f7608da3cbea2", "191e35f7e0ed4b7aaed", "13165ffa4ea7b44d109", "171976fa8a294b23c40", "1114a89792058cb4909", "121c83f7608f5f86e24", "1114a897920300abc6a", "18171adc03a638999ac", "18171adc03a6a735b25", "101d855909eb02ce5f8", "13165ffa4ea77616c06", "13165ffa4ea776126b8", "1517bfd3f76c1c63792", "141fe1da9e047508ac5", "1517bfd3f768a4a4470", "18171adc03a184b9362", "121c83f7608ffe78a2f", "191e35f7e0eaef1a4ab", "1517bfd3f7144577fee", "1517bfd3f76b014be23", "101d855909e8bbafe9e", "121c83f7608d0126826", "13165ffa4eadf621611", "101d855909ef1ef9866", "18171adc03a699035e8", "1517bfd3f76dc84e03e", "1a1018970a08c66e9d6", "13165ffa4eb0e5fa0a0", "161a3797c8afb3d0af1", "191e35f7e0e9acf207c", "18171adc03d6d7e1cfa", "18171adc03a64755bf9", "101d855909ee3c59068", "18171adc03a4c62ecce", "161a3797c8ad2f2c71d", "1a1018970a0e1ade4b5", "161a3797c8a12a39e45", "191e35f7e0ecab871dc", "1517bfd3f76807e5d3e", "1114a8979206af3952a", "171976fa8a2fad77d08", "191e35f7e0ea8ea57f1", "101d855909e81b6f6b6", "13165ffa4ead96c2329", "1114a897920c2f22f17", "1a1018970a0941a8a3f", "1114a8979202b723d5d", "191e35f7e0ee969a267", "1a1018970a0306cc6ad", "1a1018970a0df901009", "191e35f7e0eae9fd226", "1114a897920270c6b5c", "1114a8979206a9c6b94", "101d855909ebfa84f6a", "121c83f7608f3f4a385", "101d855909eca5edb52", "191e35f7e0ed74c7161", "101d855909ed999b5f8", "101d855909ef200ec95", "121c83f7608cdeb697f", "171976fa8a2bb3ac411", "1114a897920c485fa53", "141fe1da9e0133b3686", "121c83f760f3b1e3bc6", "171976fa8a2b7445415", "1a1018970a0fd87cce4", "121c83f76081c2ff600", "18171adc03a63e7d755", "13165ffa4ea31f091d7", "171976fa8a2ff79aafd", "13165ffa4eadcdd8a09", "101d855909ef240619b", "1a1018970a0fdfc67fb", "1a1018970a0e1db1739", "101d855909eb0d5981c", "1114a8979207d164bcc", "1517bfd3f76bb2fa5d2", "191e35f7e0ed1959768", "171976fa8a2d6220c47", "1517bfd3f769b67126a", "1114a8979207b80f886", "191e35f7e0ee9f40904", "1a1018970a085105397", "1a1018970a0851070eb", "101d855909ecb586653", "18171adc03a12cc1d20", "18171adc03a1b20e843", "101d855909e9af9ed51", "18171adc03a703784a9", "161a3797c8a991470d4", "161a3797c8b349f6e71", "13165ffa4ea54c5641a", "171976fa8a2e67e9b9e", "191e35f7e0ea7840fe5", "191e35f7e09b2c6322a", "1a1018970a0a4d8773e", "1a1018970a0d4ca9362", "1a1018970a0dec6cc7a", "191e35f7e0eee27a655", "191e35f7e0ed0042f6e", "1114a897927fcdb2641", "191e35f7e097ab5248a", "13165ffa4eda52fbdfe", "1a1018970a08c0c09cd", "171976fa8a2d5302a2a", "171976fa8a2baf71e46", "191e35f7e0eb81e3478", "121c83f7608a4b15386", "1114a89792052095cc7", "141fe1da9e06ea0b303", "141fe1da9e04fc35c4c", "1114a8979200e312e4c", "171976fa8a2e012376d", "191e35f7e0ec2d2b7ef", "1114a8979278f49c480", "161a3797c8ab878cbc8", "1517bfd3f76c00602d5", "1114a8979206413dc04", "191e35f7e0eee884768", "1114a897927054f6224", "121c83f7608ca201f68", "161a3797c8a99d9a785", "1517bfd3f76b9e553b5", "13165ffa4e61ff27b02", "121c83f7608a413fa2b", "191e35f7e0ee7606116", "101d855909eb6abe236", "1517bfd3f71f4993286", "121c83f7608d3d39bec", "1a1018970a0a3c0b510", "121c83f760fdab74fd3", "121c83f760889c02829", "171976fa8a2d4663c1e", "1a1018970a776e72930", "101d855909eaf221263", "18171adc03b25975608", "101d8559099717bc8d2", "13165ffa4edd3fe96a3", "161a3797c8d6e1e3712", "13165ffa4ea481c0ed4", "18171adc03d8b6cdbee", "1517bfd3f7147dbd82f", "1a1018970a0a75b2b36", "1a1018970a702ee00a8", "18171adc03a0c11b9e3", "1a1018970a768455adb", "171976fa8a2bab8f503", "121c83f760f4e2edc86", "1114a897927889e915e", "13165ffa4ed702294c1", "141fe1da9e780f194d7", "13165ffa4ed812c2ecb", "1517bfd3f710690b16c", "1a1018970a702e6488a", "121c83f760f42076c3c", "1a1018970a0a75751af", "171976fa8a2c9833eae", "191e35f7e093968b916", "1a1018970a0b09e4a6f", "171976fa8a518c1c93a", "141fe1da9e0655873fa", "1114a897927fefae179", "18171adc03a0c2a6cbd", "13165ffa4ea556c03de", "121c83f760f28a41778", "13165ffa4ed8b283cba", "161a3797c8d771744b3", "141fe1da9e7a523a11b", "1517bfd3f716e1a47c3", "121c83f760f69d06b01", "141fe1da9e7d34d52db", "101d855909956386246", "1517bfd3f717a322de9", "101d855909ea3326a7d", "101d855909971fe4d40", "141fe1da9e780ea3285", "121c83f760f73e946ab", "13165ffa4edb6d5c7a2", "141fe1da9e7b703898e", "141fe1da9e7b1159119", "1a1018970a7438d493b", "121c83f760f66a5c68f", "13165ffa4edf15284a4", "161a3797c8d3a41ff73", "101d855909ea55eb60c", "121c83f760f16e2e821", "191e35f7e0e965a2ebc", "13165ffa4ea5f8c455e", "13165ffa4edbaddf46a", "161a3797c8a85eb0176", "121c83f76089ab982bd", "171976fa8a562b43a18", "121c83f76088ce245d4", "161a3797c8d491b2210", "171976fa8a531566a8b", "161a3797c8d54c069bb", "101d8559099d95f7dc6", "101d85590990bab77f8", "161a3797c8d7d23e39e", "13165ffa4edf7a5e53b", "18171adc03dba6b197c", "1114a897927b1102201", "101d855909ea36f4023", "101d855909eafee54a8", "121c83f76089c885bc3", "191e35f7e0e8aad9018", "191e35f7e095768d959", "1a1018970a743c020c8", "121c83f760897fd6827", "1a1018970a76bd907a8", "121c83f76089a4a25db", "13165ffa4ede69f8ab5", "13165ffa4ede692dda4", "1114a897927abf552e8", "13165ffa4edf72366db", "161a3797c8d2d714cfd", "1517bfd3f7117bc7acf", "101d855909956c656e0", "101d855909929a253b8", "141fe1da9e7be54ec63", "1517bfd3f711f5ef596", "13165ffa4ea593494d7", "1114a897927bb49bd8d", "13165ffa4edadc9d91f", "1517bfd3f717f052067", "1a1018970a742bb284d", "13165ffa4ed697e308e", "1517bfd3f71222db53a", "121c83f760f65b426c2", "1517bfd3f710591ea08", "171976fa8a53662078e", "1a1018970a705fe522a", "13165ffa4edeaaad887", "1114a897927b079c981", "161a3797c8a9b0a3165", "18171adc03de8a3ef32", "101d8559099785e8fe5", "1517bfd3f7121f2422d", "101d855909966e1cb3a", "101d855909904d5d721", "13165ffa4edea44c8b8", "13165ffa4edd0807b5a", "191e35f7e09218fa356", "191e35f7e0fa1ea1fad", "1114a897927946770c2", "1114a897927aa21d57f", "121c83f760893a0d0f8", "1a1018970a0b9c2e940", "101d855909ea9095a6c", "1114a8979273228eea6", "121c83f760f1577d00e", "1114a897927923a5136", "121c83f760f58a1a670", "161a3797c8a9e2e32e6", "1517bfd3f71784672ac", "121c83f760f6828dbf5", "161a3797c8d6bfbefdb", "1114a897927bb9598a8", "1517bfd3f712101e267", "101d85590994df39c7e", "101d85590991d8844f5", "171976fa8a504af2f99", "1517bfd3f7104aa0001", "18171adc03a0a64be1d", "171976fa8a53f19b1d1", "1114a8979278b02c2bd", "171976fa8a57d7f1b96", "171976fa8a57d7d87ce", "18171adc03de71f0222", "121c83f760f71689a91", "1a1018970a779c0bf18", "171976fa8a5110c1edb", "121c83f760f476f712e", "191e35f7e0967ef7e78", "101d855909976b00b70", "13165ffa4ede44960e7", "13165ffa4ed907b7069", "121c83f760fc953e3d2", "161a3797c8d781e2743", "101d855909eaa866249", "101d855909eec7c24b9", "121c83f760f37b5cdc7", "161a3797c8d100e8fb4", "1a1018970a7318b7355", "1a1018970a762bbb6fd", "141fe1da9e7cc3bf16d", "161a3797c8d6972f4e5", "161a3797c8d6360dba3", "141fe1da9e7a7e6dd63", "101d8559099522eee4e", "18171adc03de0c3a1ac", "18171adc03db11396a6", "161a3797c8d402a3ef8", "1517bfd3f710d397526", "18171adc03db6918578", "1a1018970a731de5a3c", "121c83f760f20c78348", "161a3797c8d108b2999", "171976fa8a525c41d3d", "191e35f7e094e3f76dd", "171976fa8a56e2ba01e", "1114a897927c0f30af3", "18171adc03d8738553e", "141fe1da9e7cdb8d975", "1a1018970a0befcb21b", "18171adc038fbf6dd23", "1a1018970a712fa36fc", "18171adc03df27bf154", "161a3797c8a9c67e5d8", "1a1018970a73243a363", "1114a897927e0230cb5", "161a3797c8a9c98cc0e", "141fe1da9e7a145ca87", "1a1018970a7359a9cb9", "1a1018970a75d91d5b3", "1a1018970a7126b171d", "121c83f760f77bd5dcb", "161a3797c8d7f5b38e3", "121c83f760fbb118db1", "121c83f760f367f9be1", "171976fa8a5a1bbc8e5", "141fe1da9e7ea980cc4", "121c83f760f1e3db3ce", "141fe1da9e7d94e626a", "171976fa8a53dc1a34b", "13165ffa4ed40d687c3", "13165ffa4ed866bdfcb", "141fe1da9e711e3a729", "121c83f760f7940c117", "1114a897927e774c5d7", "121c83f760f4569a976", "1a1018970a7c55fca92", "121c83f760ff11f28ec", "191e35f7e09fe6fcdcc", "171976fa8a5a029146d", "171976fa8a519c9f4d7", "18171adc03da30cbfdc", "1114a897927b47bfc5c", "1a1018970a1b25fde2d", "141fe1da9e783d45c4b", "121c83f760f3398b2fe", "191e35f7e0941a10101", "141fe1da9e7bbf63d35", "121c83f760fee85459e", "121c83f760f42c59e18", "1517bfd3f7164c791e6", "141fe1da9e7b7c7eef8", "191e35f7e09041ac173", "13165ffa4ede23777bf", "141fe1da9e7bbcf2286", "161a3797c8d3db109dd", "1a1018970a77aeb67f6", "161a3797c8d58504c6c", "18171adc03d85e25c61", "121c83f760f28ef89be", "171976fa8a55af4e01f", "141fe1da9e7085cbcb2", "1517bfd3f712d50de36", "18171adc03d7db4a157", "18171adc03d43f00543", "171976fa8a5fdd3ef3c", "1a1018970a71f77ed1e", "1517bfd3f7199aa6fba", "171976fa8a5a50f84e9", "1a1018970a7d745add0", "171976fa8a597984d21", "141fe1da9e70fe13b72", "13165ffa4ed28f8878f", "191e35f7e09c8557c10", "13165ffa4ed3f8958a1", "18171adc03d4e958812", "171976fa8a5a5a8c038", "191e35f7e09c44cb14f", "1a1018970a78abfa3ea", "1114a8979272412183d", "1114a89792757c06476", "141fe1da9e718706339", "13165ffa4ed3245bb79", "171976fa8a590241ce9", "1a1018970a79c8e2ff0", "121c83f760a501c619d", "18171adc03d601400b2", "141fe1da9e709670f4f", "121c83f760ffc5e3e05", "191e35f7e09e6fcca42", "1517bfd3f71c36a6a42", "171976fa8a590fbdc82", "1a1018970a7d61caa02", "101d8559099d2c1e914", "1114a897927743f4b54", "1517bfd3f719e5154f2", "1114a8979272face0bd", "1a1018970a7a5590dd3", "171976fa8a5be9e89c0", "1114a89792724a04410", "141fe1da9e75f5f3625", "161a3797c8d8a30f8ab", "18171adc03d2a1b8043", "121c83f760fc3f4842a", "141fe1da9e74d6f373d", "191e35f7e09f649c925", "161a3797c8da4a3122c", "1a1018970a7804e5710", "13165ffa4ed47aadccb", "1a1018970a7f1bbbe4c", "13165ffa4ed2cc5594d", "141fe1da9e730c1b1ba", "121c83f760fdbe2029e", "121c83f760fb82f931f", "141fe1da9e70ca75e74", "161a3797c8dc74e1f53", "121c83f760fec7f464c", "171976fa8a58105b775", "171976fa8a5f74e5232", "141fe1da9e7036e6503", "141fe1da9e740eac7b4", "1517bfd3f71ba6b57de", "1517bfd3f71d86f6d1a", "141fe1da9e70c4d4ba7", "171976fa8a595f4bebd", "13165ffa4ed145beb0c", "191e35f7e09e39b3287", "121c83f760fb376e015", "171976fa8a5bd97f69c", "101d855909989a69873", "161a3797c8def67529a", "1517bfd3f71f1379d54", "161a3797c8d85067d6f", "1517bfd3f71b62d77a8", "18171adc03d5e8d39d5", "1517bfd3f71e99a09f1", "1114a8979276bfc6cbf", "191e35f7e09ef488a90", "191e35f7e09c1a65294", "18171adc03d17cacc46", "1a1018970a7f7404d90", "171976fa8a5bc4af743", "18171adc03d17c0732a", "191e35f7e09a5946d6c", "1114a8979272b076496", "141fe1da9e746244f4a", "1517bfd3f71cc9d17fa", "1114a8979277e47a392", "191e35f7e099127d887", "1a1018970a7e49126bc", "191e35f7e099121215f", "171976fa8a5c1e2f091", "191e35f7e099be1c0a3", "1517bfd3f71dd0956e1", "1114a8979271632f7a1", "1517bfd3f71f056a78b", "18171adc03d28be6c7c", "1517bfd3f71e4a3a183", "161a3797c8dd4701aed", "1a1018970a782e7db2c", "1114a8979271ae87d66", "1517bfd3f71c76fd7eb", "171976fa8a58ae7f870", "1a1018970a7f0d50fe2", "1517bfd3f71bf001bcd", "1114a8979276c100601", "171976fa8a5868b11c4", "161a3797c8dc6fd9de4", "141fe1da9e7136a539f", "1a1018970a79e0c3ea7", "171976fa8a2d8d1fe37", "1a1018970a7e1af8047", "171976fa8a5ce4e3a08", "13165ffa4ed4ffb2147", "1a1018970a7e1ac0f7e", "141fe1da9e734e88c70", "101d8559099cb315e13", "18171adc03d284b0443", "191e35f7e09e81aefc7", "161a3797c8d8442a05f", "1a1018970a7eb902d1c", "13165ffa4ed1cc6463e", "13165ffa4ed379f2051", "171976fa8a5d5153231", "1a1018970a7ebe43c19", "18171adc03d3faeae6c", "161a3797c8ddf2b6817", "171976fa8a5bb166e82", "121c83f760fe606ca84", "191e35f7e09c1aa7456", "101d8559099e62a0003", "141fe1da9e7387808ff", "1517bfd3f71b86c0a7e", "1a1018970a7a07b0b04", "13165ffa4ed317fadc9", "141fe1da9e77f2bae76", "18171adc03d2798c05f", "1a1018970a79bf7ccb0", "1517bfd3f71898a1f66", "1114a89792749bb478b", "18171adc03d68625834", "1114a8979270fd77006", "1114a89792727bb7853", "121c83f760fe5894b20", "1517bfd3f71f935c683", "141fe1da9e7107e8b3f", "191e35f7e09abf79741", "121c83f760fd92ddab4", "171976fa8a5d44d0c1c", "1517bfd3f71a7e0c2de", "161a3797c8deda51a76", "121c83f760ffebfb7c2", "171976fa8a5f9a5ed8d", "1114a8979277d8eca9d", "18171adc03d269b1a04",
                "141fe1da9e745c40b93",//我的设备 185 无token
                "101d8559099f594d5d0"//我的设备 173 有token
            ];

            //少的
            $regids_few = [
                "141fe1da9e745c40b93",//我的设备 185 无token
                "101d8559099f594d5d0",//我的设备 173 有token
                "171976fa8a51579efd5",
            ];


//            $user_pushs = (new UserPush())->whereIn('regid', $regids)->field('regid', 'device_token')->buildSql(); //去线上数据库查询（时间为四秒多，可以接受）

            //推送数据
            $data1 = json_encode('{"plantform":1,"pushType":1,"regid":["1507bfd3f797c89c:"准了","content":"您有一条新的回复，请查收","extras":{"target":"local","link":"reply_me?need_auth=2","push_id":"5245","push_type":"个推","push_content":"您有一条新的回复，请查收"},"informType":0,"bigText":"","bigPicPath":"","timeToLive":86400,"device_tokens":""}');

            $data = [
                'plantform' => 1, // 1 => all  2 => IOS 3 => android
                'title' => '准了', // 推送标题
                'content' => '选择往往比努力更重要，试试职业性格评测，看看你的性格更适合什么样的工作？', // 推送内容
                'extras' => [
                    'target' => 'local',
                    'link' => 'mental_test_detail?page=mental_test_detail&id=193&need_auth=2',
                    'push_id' => 1,
                    'push_type' => '个推',
                    'push_content' => '选择往往比努力更重要，试试职业性格评测，看看你的性格更适合什么样的工作？',
                ], // 扩展字段
                'informType' => 0, //通知栏样式 0默认 1大文本 2收件箱 3大图片
                'bigText' => '', // 大文本内容
                'bigPicPath' => '', // 大图片路径
                'timeToLive' => 86400, // 默认86400
                'biTag' => ''
            ];

            $is_anps_send_success = false;//是否通过apns发送
            if (config('push.apns')['is_use'])
            {
                //所有发送的数据
                $device_tokens_all = (new UserPush())->whereIn('regid', $regids_few)->column('regid,device_token');
                //apns发送的数据（device_token 数组）
                $device_tokens_apns = array_values(array_unique(array_filter($device_tokens_all)));
                //jpush发送的数据（regid 数组）
                $regids_jpush = array_keys(array_diff($device_tokens_all, $device_tokens_apns));

                if (!empty($device_tokens_apns))
                {//apns推送
                    $apnsPush = new ApnsPush();
                    $res_apns = $apnsPush->setData($data)->push($device_tokens_apns);
                    if ((int)$res_apns['http_code'] === 200)
                    {
                        $res_apns['success'] = true;
                        $is_anps_send_success = true;
                    } else
                    {
                        $res_apns['success'] = false;
                    }

                }
            }

            //apns发送失败，还原极光推送所有的数据
            if (!$is_anps_send_success)
            {
                $regids_jpush = $regids_few;
            }

            //极光推送
            try
            {
                $jPush = new Jpush(config('push')['jpush']);
                $res = $jPush->setData($data)->push($regids_jpush);

                if ((int)$res['http_code'] !== 200)
                {
                    Log::write('极光推送失败；错误消息：' . json_encode($res) . ',推送设备ID：' . json_encode($regids_jpush) . ',推送消息：' . json_encode($data), 'error');
                    $md = ['code' => 500, 'msg' => $res];
                } else
                {
                    $md = ['code' => 200, 'msg' => $res];
                    Log::write('极光推送成功；成功消息：' . json_encode($res) . ',推送设备ID：' . json_encode($regids_jpush) . ',推送消息：' . json_encode($data), 'error');
                }
            }
            catch (\Exception $e)
            {
                Log::write('极光推送失败；错误消息：' . $e->getMessage() . ',推送设备ID：' . json_encode($regids_jpush) . ',推送消息：' . json_encode($data), 'error');
                $this->sendMsg('极光推送失败', "## 极光推送异常\r\n > 错误消息：" . $e->getMessage());
                $md = ['code' => 500, 'msg' => $e->getMessage()];
            }

            //个推
//            if (config('push.apns')['is_use'])
//            {
//                if (!isset($data['is_jpush']) || (isset($data['is_jpush']) && (int)$data['is_jpush'] === 0))
//                {
//                    if (!isset($data['device_tokens']) && isset($data['regid']))
//                    {//如果 device_tokens 为空，并且regid不为空的话，那么进行查询device_tokens
//                        if (is_array($data['regid']) && count($data['regid']) === 1)
//                        {
//                            $user_push = (new UserPush())->field('device_token,push_type,regid,character_status')->where(['regid' => $data['regid'][0]])->find();
//                            $device_token = $user_push['device_token'] ?? '';
//                        } else
//                        {
//                            Log::write('个推：regid count > 0，params=' . $data['regid'], 'error', true);
//                        }
//                    } else
//                    {//设置了 device_tokens
//                        $device_token = $data['device_tokens'];
//                    }
//
//                    if (!empty($device_token))
//                    {//apns推送
//                        $apnsPush = new ApnsPush();
//                        $res = $apnsPush->setData($data)->push($device_token);
//                        if ((int)$res['http_code'] === 200)
//                        {
//                            $res['success'] = true;
//                        } else
//                        {
//                            $res['success'] = false;
//                        }
//
//                        return $res;
//                    }
//                }
//            }

            return show(0, '查询结果', [
                'device_tokens_all' => $device_tokens_all,
                'device_tokens_apns' => $device_tokens_apns,
                'regids_jpush' => $regids_jpush,
                'jpush_result' => $md,
                'apns_reuslt' => $res_apns
            ]);
        }
    }


}
